package com.smallkingson.ylib.view.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.smallkingson.ylib.view.listener.YPositionOnClickListener;

import java.util.HashMap;
import java.util.Map;


public abstract class YHolderAdapter extends YBaseAdapter {

    private final Context mContext;
    public SparseArray<View> holder;

    public View itemView;

    private Map<String, Object> tags;

    public YHolderAdapter(Context context) {
        super();
        mContext = context.getApplicationContext();
        tags = new HashMap<String, Object>();
    }

    @SuppressWarnings({"deprecation", "unchecked"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = null;

        if (convertView == null) {

            view = LayoutInflater.from(mContext).inflate(getLayoutRes(position), null);

            SparseArray<View> array = new SparseArray<View>();

            view.setTag(array);

        } else {

            view = convertView;
        }

        holder = (SparseArray<View>) view.getTag();

        itemView = view;

        // 设置点击事件
        if (!(view instanceof AdapterView)) {

            view.setOnClickListener(new YPositionOnClickListener(position) {

                @Override
                public void onClick(View v) {
                    onItemClick(v, this.position);
                }
            });
        }
        setUpView(position, view);

        return getView(position, view);

    }

    /**
     * 需要时覆盖
     *
     * @param v
     * @param position
     */
    protected void onItemClick(View v, int position) {

    }

    protected abstract void setUpView(int position, View layout);

    public Button $btn(int id) {

        Button button = (Button) $(id);

        return button;
    }

    public ViewGroup $vp(int id) {

        ViewGroup viewGroup = (ViewGroup) $(id);

        return viewGroup;
    }

    public TextView $text(int id) {

        TextView text = (TextView) $(id);

        return text;
    }

    public CheckBox $checkBox(int id) {

        CheckBox cb = (CheckBox) $(id);

        return cb;
    }

    public ImageView $img(int id) {

        ImageView img = (ImageView) $(id);

        return img;
    }

    public View $(int id) {

        View view = holder.get(id, null);
        if (view == null) {
            view = itemView.findViewById(id);
            holder.put(id, view);
        }
        return view;
    }

    @Deprecated
    public View getView(int position, View layout) {
        return layout;
    }

    public Object getTag(String key) {
        return tags.get(key);
    }

    public YHolderAdapter putTag(String key, Object value) {
        tags.put(key, value);
        return this;
    }

    public abstract int getLayoutRes(int position);

}